import grails.util.GrailsUtil
import org.leaf.green.b2b.*
import org.leaf.green.shiro.*
import org.leaf.green.admin.*
import org.apache.shiro.crypto.hash.Sha512Hash

class bBootStrap {

    def init = { servletContext ->
        switch(GrailsUtil.environment) {
            case "development":
                configureForDevelopment()
                break
            case "production":
                configureForDevelopment()
                break
        }
    }
    def destroy = {
    }

    def configureForDevelopment = {

        /*init retailer inventory*/
        Retailer.listOrderBySerialNumber().each{ retailer ->
            def ri_today = new RetailerInventory(retailer : retailer, date : new Date())
            def ri_tomorrow = new RetailerInventory(retailer : retailer, date : new Date() + 1)
            Product.listOrderBySerialNumber().each{ product ->
                ri_today.addToDetails(
                    new RetailerInventoryDetail(
                          product : product
                        , quantity : 0
                        , price : 0
                        , amount : 0
                    )
                )
                ri_tomorrow.addToDetails(
                    new RetailerInventoryDetail(
                          product : product
                        , quantity : 0
                        , price : 0
                        , amount : 0
                    )
                )
            }
            ri_today.save(flush:true)
            ri_tomorrow.save(flush:true)
        }
        /*init package*/

        new Package(serialNumber : '100', name : '中筐', isActive : false).save(flush:true)
        new Package(serialNumber : '101', name : '小筐', isActive : false).save(flush:true)
        new Package(serialNumber : '102', name : '白筐', isActive : false).save(flush:true)
        new Package(serialNumber : '103', name : '圆白筐', isActive : false).save(flush:true)

        /*init purchase*/
        
        def recordType_purchase = new RecordType (serialNumber : '1', name : '采购单', domain : 'purchase',  isActive : true, description : '').save(flush:true)
        def recordType_pickplan = new RecordType (serialNumber : '2', name : '采摘计划', domain : 'purchase',  isActive : true, description : '').save(flush:true)
        
        def purchase_purchase = new Purchase(
              recordType : recordType_purchase
            , serialNumber : 'test001'
            , date : new Date()
            , dealer : Dealer.findWhere(serialNumber : 'SH0001')
            , maker : Staff.get(2)
            , director : Staff .get(1)
        )
        def begin_serialNumber = 0
        Product.listOrderBySerialNumber().each{ product ->
            begin_serialNumber += 1
            purchase_purchase.addToDetails(
                new PurchaseDetail(
                      serialNumber : String.format('%03d', begin_serialNumber)
                    , dealer : purchase_purchase.dealer
                    , product : product
                    , price : 88 * begin_serialNumber
                    , quantity : 100
                    , amount : 8800
                    , unit : '斤'
                )
            )
        }
        //purchase_purchase.quantity = purchase_purchase.details*.quantity.sum()
        //purchase_purchase.amount   = purchase_purchase.details*.amount.sum()
        purchase_purchase.save(flush:true)

        def dealer_for_purchase_pickplan_detail = Dealer.findWhere(serialNumber : 'A1')
        log.info("dealer_for_purchase_pickplan_detail=${dealer_for_purchase_pickplan_detail}")
 
        def purchase_pickplan = new Purchase(
              recordType : recordType_pickplan
            , serialNumber : 'test002'
            , date : new Date()
            , dealer : null
            , maker : Staff.get(2)
            , director : Staff .get(1)
        )
        begin_serialNumber = 0
        Product.listOrderBySerialNumber().each{ product ->
            begin_serialNumber += 1
            purchase_pickplan.addToDetails(
                new PurchaseDetail(
                      serialNumber : String.format('%03d', begin_serialNumber)
                    , dealer : dealer_for_purchase_pickplan_detail
                    , product : product
                    , quantity : 10000
                    , price : 88
                    , amount : 8800
                    , unit : '斤'
                )
            )
        }
        //purchase_pickplan.quantity = purchase_pickplan.details*.quantity.sum()
        //purchase_pickplan.amount   = purchase_pickplan.details*.amount.sum()
 
        purchase_pickplan.save(flush:true)

        /*init distribution*/
        def distributionInstance = new Distribution()
            //set serialnumber
            distributionInstance.serialNumber = "${new Date().format('yyyyMMdd')}001"
            //set approved 
            distributionInstance.approved = false

            distributionInstance.retailer = Retailer.get(1)
            distributionInstance.date = new Date()
            distributionInstance.maker = Staff.get(1)
            distributionInstance.driver = Staff.get(2)
          
        //add details
        begin_serialNumber = 0
        Product.listOrderBySerialNumber().each{ product ->
            begin_serialNumber += 1
            distributionInstance.addToDetails(
                new DistributionDetail(
                      serialNumber : String.format('%03d', begin_serialNumber)
                    , product : product
                    , unit : '斤'
                    , quantity : 100
                    , sended : 0
                    , price : 10
                    , retailPrice : 15
                    , amount : 1000
                    , retailAmount : 1500
                    , packages : Package.get(1)
                    , packageUsed : 10
                    , packageReturned : 0
                )
            )
        }
        distributionInstance.save(flush:true)
        /*init stock out*/
        /*
        new StockOut(serialNumber : 'SO-20120210-0001', retailer : Retailer.get(1), date : new Date(), maker : Staff.get(1), driver : Staff.get(2))
            .addToDetails(new StockOutDetail(
                    serialNumber : '001'
                  , product : Product.get(1)
                  , packages : Package.get(1)
                  , unit : '斤'
                  , quantity : 100.00
                  , price : 2.80
                  , retailPrice : 3.00
                  , amount : 280
                  , retailAmount : 300
                  , packageUse : 2
                  , packageReturn : 0
                )
            ).addToDetails(new StockOutDetail(
                    serialNumber : '002'
                  , product : Product.get(2)
                  , packages : Package.get(2)
                  , unit : '斤'
                  , quantity : 100.00
                  , price : 2.80
                  , retailPrice : 3.00
                  , amount : 280
                  , retailAmount : 300
                  , packageUse : 2
                  , packageReturn : 0
                )
            ).addToDetails(new StockOutDetail(
                    serialNumber : '003'
                  , product : Product.get(3)
                  , packages : Package.get(3)
                  , unit : '斤'
                  , quantity : 100.00
                  , price : 2.80
                  , retailPrice : 3.00
                  , amount : 280
                  , retailAmount : 300
                  , packageUse : 2
                  , packageReturn : 0
                )
            ).save(flush:true)
        */
    }
}
